//
//  LoginViewController.m
//  DialectShare
//
//  Created by timmy zhong on 3/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "LoginViewController.h"
#import "UserProxy.h"
#import "TinyView.h"
#import "User.h"
#import "RegisterViewController.h"

@interface LoginViewController ()
@property (nonatomic, retain) UserProxy *proxy;
@end

@implementation LoginViewController
@synthesize account = _account, password = _password;
@synthesize proxy = _proxy;

- (void)dealloc
{
    self.account = nil;
    self.password = nil;
    self.proxy.delegate = nil;
    self.proxy = nil;
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"登陆";
    _proxy = [[UserProxy alloc] init];
    _proxy.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)toLogin:(id)sender
{
    NSString *account = self.account.text;
    NSString *password = self.password.text;
    if (account == @"" || password == @"" || account == nil || password == nil) {
        [TinyView autoDismissMessageWithTarget:self.view timeinterval:1.0f message:@"帐号或密码不能为空"];
        return ;
    }
    
    User *user = [[User alloc] init];
    user.account = account;
    user.password = password;
    [_proxy login:user];
    [user release];
}

- (IBAction)toRegister:(id)sender
{
    RegisterViewController *registerController = [[RegisterViewController alloc] init];
    [self presentModalViewController:registerController animated:YES];
    [registerController release];
}

#pragma mark - Proxy delegate
- (void)requestFailWithProxy:(Proxy *)proxy
{
    
}

- (void)requestFinishWithProxy:(Proxy *)proxy
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - text field
- (void)keyboardWillShow:(NSNotification *)noti
{        
    CGRect frame = self.view.frame;        
    if (frame.origin.y < 0) {
        return ;
    }
    frame.origin.y -= 100;
    [UIView beginAnimations:@"Curl"context:nil];//动画开始          
    [UIView setAnimationDuration:0.30f];           
    [UIView setAnimationDelegate:nil];          
    [self.view setFrame:frame];         
    [UIView commitAnimations];         
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{        
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];        
    [UIView setAnimationDuration:0.30f];        
    CGRect rect = self.view.frame;
    rect.origin.y += 100;
    self.view.frame = rect;        
    [UIView commitAnimations];        
    [textField resignFirstResponder];
    return YES;        
}



@end
